package com.googlecode.gwt_control.client.form;

import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.FocusListenerAdapter;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

/**
 *
 * @author Olafur Gauti Gudmundsson
 */
public class TextField extends Field {

    private final TextBox box;

    public TextField( String id, String title ) {
        this(id, title, null);
    }

    public TextField( String id, String title, FieldValidator validator ) {
        super(id, title, validator);
        this.box = new TextBox();
        box.setStyleName("textField");

        // make sure change events are captured
        box.addFocusListener(new FocusListenerAdapter() {
            @Override
            public void onLostFocus(Widget w) {
                fireFieldChanged();
            }
        });
    }

    @Override
    public void addToTable(int rowNumber, FlexTable table) {
        super.addToTable(rowNumber, table, box);
    }

    @Override
    public void setEnabled(boolean enabled) {
        super.setEnabled(enabled);
        box.setEnabled(enabled);
        if ( enabled ) {
            box.removeStyleName("disabled");
        } else {
            box.addStyleName("disabled");
        }
    }

    public TextBox getBox() {
        return box;
    }

    public String getText() {
        return box.getText();
    }

    public void setText( String text ) {
        box.setText(text);
    }

    @Override
    public boolean isEmpty() {
        return getText().trim().length() == 0;
    }

}
